package com.jxstyle.core.base;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.jxstyle.core.exception.BizExceptionEnum;
import com.jxstyle.core.response.ErrorResponse;
import com.jxstyle.core.response.Response;
import com.jxstyle.core.security.SecurityUser;
import com.jxstyle.modular.entity.User;

public class BaseController {

	private static Response<Object> successResponse = new Response<>();
	
	@SuppressWarnings("rawtypes")
	protected Response success() {
		return successResponse;
	}
	protected Response<?> success(Object data) {
		Response<?> successResponse = new Response<>();
		successResponse.setData(data);
		return successResponse;
	}

	@SuppressWarnings("rawtypes")
	protected Response error(int code,String errorMsg) {
		return new ErrorResponse(code,errorMsg);
	}
	@SuppressWarnings("rawtypes")
	protected Response error(int code) {
		return this.error(code, null);
	}
	@SuppressWarnings("rawtypes")
	protected Response error(BizExceptionEnum bizExceptionEnum) {
		return this.error(bizExceptionEnum.getCode(), bizExceptionEnum.getMessage());
	}
	protected User getUser() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		SecurityUser securityUser = (SecurityUser)authentication.getPrincipal();
		return securityUser.getDetail();
	}
}
